package com.neutech.dao.impl;

import com.neutech.constant.Constant;
import com.neutech.dao.BusinessDAO;
import com.neutech.entity.Business;
import com.neutech.entity.Food;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDAOlmpl implements BusinessDAO {


    private QueryRunner queryRunner = new QueryRunner(Constant.dataSource);


    //执行查询所有商家的方法实现
    @Override
    public List<Business> selectAll() {
        List<Business> businessesList = new ArrayList<>();
        try {
            queryRunner.execute("select * from business", new ResultSetHandler<Object>() {
                @Override
                public Object handle(ResultSet resultSet) throws SQLException {
                    while (resultSet.next()){
                        Business business = new Business();
                        business.setBusinessId(resultSet.getInt("business_id"));
                        business.setPassword(resultSet.getString("password"));
                        business.setBusinessName(resultSet.getString("business_name"));
                        business.setBusinessAddress(resultSet.getString("business_address"));
                        business.setBusinessExplain(resultSet.getString("business_explain"));
                        business.setStartPrice(resultSet.getBigDecimal("start_price"));
                        business.setDeliveryPrice(resultSet.getBigDecimal("delivery_price"));
                        businessesList.add(business);
                    }
                    return null;
                }
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return businessesList;
    }

    //实现接口中查找商家姓名方法的重写
    @Override
    public Business selectBusinessName(String businessName) {
        final Business business = new Business();
        try {
            queryRunner.execute("select * from business where business_name = ?", new ResultSetHandler<Object>() {
                @Override
                public Object handle(ResultSet resultSet) throws SQLException {
                    if(resultSet.next()){
                        business.setBusinessId(resultSet.getInt("business_id"));
                        business.setPassword(resultSet.getString("password"));
                        business.setBusinessName(resultSet.getString("business_name"));
                        business.setBusinessAddress(resultSet.getString("business_address"));
                        business.setBusinessExplain(resultSet.getString("business_explain"));
                        business.setStartPrice(resultSet.getBigDecimal("start_price"));
                        business.setDeliveryPrice(resultSet.getBigDecimal("delivery_price"));
                    }
                    return null;
                }
            },businessName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return business;

    }

    @Override
    public int add(Business business) {
        int row = 0;
        try {
           row = queryRunner.execute("insert into business (password,business_name,business_address," +
                    "business_explain,start_price,delivery_price) values (?,?,?,?,?,?)",business.getPassword()
            ,business.getBusinessName(),business.getBusinessAddress(),business.getBusinessExplain(),business.getStartPrice()
            ,business.getDeliveryPrice());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }


    //对接口删除商家方法的实现
    public int delete(Business business) {
        int row = 0;
        try {
           row = queryRunner.execute("delete from business where business_name = ?",business.getBusinessName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }




}
